import http from '../utils/http'
/**
 * 判断对象是否为空
 */
function isEmptyObject(obj) {
    return Object.keys(obj).length === 0;
}
/**
 * 获取全局CsrfToken
 */
async function getCsrToken() {
    // localStorage.setItem("X-Csrf-Token", '');
    return await new Promise((resolve, reject) => {
        if (!localStorage.getItem('X-Csrf-Token')) {
            console.log('不存在 请求token')
            http.get('getCsrfToken').then(res => {
                if (res.code == 0) {
                    localStorage.setItem("X-Csrf-Token", res.token);
                }
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        }
    })
}
//下载文件
function downloadFile(url, name) {
    const link = document.createElement('a');
    link.style.display = 'none';
    link.download = name;
    link.href = url;
    link.click();
    document.body.removeChild(link);
}

// 获取级联目录
/**
 * 
 * @param {*} list 分类数组
 * @param {*} ids 所在的分类
 */
function getLevelData(list, ids) {
    // console.log('ids', ids)
    var str = ''
    var len = ids.length
    list.forEach(x => {
        if (len == 1) {
            if (x.category_id == ids[0]) {
                str += x.name
            }
        } else if (len == 2) {
            if (x.category_id == ids[0]) {
                str += x.name
                if (x.children) {
                    x.children.forEach(x1 => {
                        if (x1.category_id == ids[1]) {
                            str += ' / ' + x1.name
                        }
                    })
                }
            }
        } else if (len == 3) {
            if (x.category_id == ids[0]) {
                str += x.name
                if (x.children) {
                    x.children.forEach(x1 => {
                        if (x1.category_id == ids[1]) {
                            str += ' / ' + x1.name
                            if (x1.children) {
                                x1.children.forEach(x2 => {
                                    if (x2.category_id == ids[2]) {
                                        str += ' / ' + x2.name
                                    }
                                })
                            }
                        }
                    })
                }
            }
        }
    })
    return str
}

/**
 * 导出 
 **/
export {
    isEmptyObject,
    getCsrToken,
    downloadFile,
    getLevelData
}

